﻿using Fishing.Abstractions;
using Fishing.View;
using System.Windows;

namespace Fishing.Core
{
    internal class ProgramController : IProgramController
    {
        private readonly MainWindow _mainWindow;
        private readonly IViewProvider _viewProvider;

        public ProgramController(MainWindow mainWindow, IViewProvider viewProvider)
        {
            _mainWindow = mainWindow;
            _viewProvider = viewProvider;
        }

        public void Exhale()
        {
            if (_mainWindow.IsVisible)
            {
                if (_mainWindow.WindowState == WindowState.Normal)
                {
                    return;
                }

                _mainWindow.WindowState = WindowState.Normal;
            }

            ExhaleCore();
        }

        public void Hide()
        {
            if (!_mainWindow.IsVisible)
            {
                return;
            }

            HideCore();
        }

        public void Toggle()
        {
            if (_mainWindow.IsVisible)
            {
                HideCore();
            }
            else
            {
                ExhaleCore();
            }
        }

        public void Exit()
        {
            Application.Current.Shutdown();
        }

        private void ExhaleCore()
        {
            _mainWindow.ShowInTaskbar = true;
            _mainWindow.Show();
            _mainWindow.WindowState = WindowState.Normal;

            if (_viewProvider.ExistsView<SettingsView>())
            {
                _viewProvider.GetView<SettingsView>().Show();
            }
        }

        private void HideCore()
        {
            _mainWindow.ShowInTaskbar = false;
            _mainWindow.Hide();

            if (_viewProvider.ExistsView<SettingsView>())
            {
                _viewProvider.GetView<SettingsView>().Hide();
            }
        }
    }
}
